//
//  ConsultationViewController.m
//  ChineseCollegeStudentsNetwork
//
//  Created by 刘喆 on 2018/5/9.
//  Copyright © 2018年 刘喆. All rights reserved.
//

#import "ConsultationViewController.h"

#import "SearchView.h"
#import "BannerView.h"
#import "SectionView.h"

#import "CampusPersonageTableViewCell.h"
#import "CommonModel.h"
#import "MLAWebVC.h"
#import "ConsultationCateListVC.h"
#import "SearchResultVC.h"

static NSString *kcampusCell = @"CampusCell";

@interface ConsultationViewController ()<UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) SearchView *searchView;
@property (nonatomic, strong) SDCycleScrollView *headView;

@property (nonatomic, copy) NSArray *bannersArr;

@property (nonatomic, copy) NSArray *listArr;


@end

@implementation ConsultationViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
- (instancetype)init {
    self = [super init];
    if (self) {
        self.needHideNavigationBar = YES;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupUI];
    [self setupLayout];
    
    [MLAHttpManage postHttpType:MLAHttpRequestType_Get hostType:MLAHttpHostType_Api api:@"/api/v1/stl/contents" params:@{@"channelName":@"首页Banner"} success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        _bannersArr = [CommonModel mj_objectArrayWithKeyValuesArray:[(NSDictionary *)response objectForKey:@"value"]];
        NSMutableArray *listArr = [NSMutableArray array];
        for (CommonModel *model in _bannersArr) {
            [listArr addObject:model.imageUrl];
        }
        _headView.imageURLStringsGroup = listArr;
        
    } failure:^{
        
    }];

    [MLAHttpManage postHttpType:MLAHttpRequestType_Get hostType:MLAHttpHostType_Api api:@"/api/v1/contents/32?channelIds=36,37&orderBy=addDate%20desc" params:nil success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        _listArr = [CommonModel mj_objectArrayWithKeyValuesArray:response[@"value"]];
        [self.tableView reloadData];
    } failure:^{

    }];
}

#pragma mark - UI

- (void)setupUI {
    self.searchView = [[SearchView alloc] init];
    [self.searchView didSearchTextString:^(NSString *text) {
#pragma mark 搜索
        SearchResultVC *resultVC = [[SearchResultVC alloc] init];
        resultVC.keyword = text;
        [self.navigationController pushViewController:resultVC animated:YES];
    }];
}

- (void)setupLayout {
    [self.view addSubview:self.searchView];
    [self.searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(15);
        make.height.mas_equalTo(64);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.offset(0);
        make.top.equalTo(self.searchView.mas_bottom);
    }];
}

- (SDCycleScrollView *)headView {
    if(!_headView){
        _headView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.view.frame.size.width/1.875) delegate:self placeholderImage:[UIImage imageNamed:@"飞艇"]];
        _headView.clickItemOperationBlock = ^(NSInteger currentIndex) {
            CommonModel *model = _bannersArr[currentIndex];
            MLAWebVC *webVC = [[MLAWebVC alloc] init];
            webVC.urlStr = model.navigationUrl;
            webVC.titleStr = model.title;
            [self.navigationController pushViewController:webVC animated:YES];
        };
    }
    return _headView;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = UIColor.whiteColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedSectionHeaderHeight = 0.001;
        _tableView.estimatedSectionFooterHeight = 0.001;
        [_tableView registerClass:[CampusPersonageTableViewCell class] forCellReuseIdentifier:kcampusCell];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"1"];
        self.headView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        _tableView.tableHeaderView = self.headView;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        [self.view addSubview:self.tableView];
    }
    return _tableView;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _listArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CampusPersonageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kcampusCell forIndexPath:indexPath];
    
    CommonModel *model = _listArr[indexPath.item];
    [cell.iconImgv sd_setImageWithURL:[NSURL URLWithString:model.imageUrl] placeholderImage:[UIImage imageNamed:@"飞艇"]];
    cell.timeLbl .text = model.addDate;
    cell.titleLbl.text = model.title;;
    cell.contextLbl.text = model.summary;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 140;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 100;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.0001;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *view = [[UIView alloc] init];
    CGFloat width = SCREEN_WIDTH/5.;
    view.frame = CGRectMake(0, 0, self.view.frame.size.width, width*1.285);
    
    NSArray *arr = @[@"ic_special",@"ic_news",@"ic_school",@"ic_liuxue",@"ic_high_school"];

    [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIImageView *imgv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:obj]];
        imgv.userInteractionEnabled = YES;
        imgv.frame = CGRectMake(width*idx, 0, width, width*1.285);
        imgv.tag = 10000+idx;
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushToCateList:)];
        [imgv addGestureRecognizer:tapGes];
        [view addSubview:imgv];
    }];
    
    return view;
}


- (void)pushToCateList:(UITapGestureRecognizer *)tapGes {
    NSInteger index = tapGes.view.tag - 10000;
    NSString *api;
    NSString *title;
    switch (index) {
        case 0:
        {
            api = @"/api/v1/contents/32/34";
            title = @"特别报道";
        }
            break;
        case 1:
        {
            api = @"/api/v1/contents/32/36";
            title = @"新闻";

        }
            break;
        case 2:
        {
            api = @"/api/v1/contents/32/37";
            title = @"校园";

        }
            break;
        case 3:
        {
            api = @"/api/v1/contents/32/38";
            title = @"留学";

        }
            break;
        case 4:
        {
            api = @"/api/v1/contents/32/342";
            title = @"高校聚焦";

        }
            break;

        default:
            break;
    }
    
    
    ConsultationCateListVC *cclVC = [[ConsultationCateListVC alloc] init];
    cclVC.title = title;
    cclVC.apiStr = api;
    [self.navigationController pushViewController:cclVC animated:YES];
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CommonModel *model = _listArr[indexPath.row];
    MLAWebVC *webVC = [[MLAWebVC alloc] init];
    webVC.urlStr = model.navigationUrl;
    webVC.titleStr = model.title;
    [self.navigationController pushViewController:webVC animated:YES];
}

@end
